package com.inspinia.auth.sso.server.controller;

import com.baomidou.kisso.SSOHelper;
import com.baomidou.kisso.SSOToken;
import com.baomidou.kisso.annotation.Action;
import com.baomidou.kisso.annotation.Login;
import com.inspinia.base.util.JsonUtils;
import com.inspinia.upms.common.model.user.UpmsUser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

import static com.inspinia.auth.sso.server.controller.SsoController.loadLoginSetting;

@Controller
public class SsoIndexController {



    /**
     * 跳转
     * @param url
     * @return
     */
    public String redirectTo(String url) {
        StringBuffer rto = new StringBuffer("redirect:");
        rto.append(url);
        return rto.toString();
    }

    /**
     * 单点中心首页
     * @return
     */
    @Login(action = Action.Skip)
    @RequestMapping("/")
    public String home(HttpServletRequest request, Model model) {
        SSOToken token = SSOHelper.getToken(request);
        if (Objects.nonNull(token)){
            UpmsUser user = JsonUtils.fromJson(token.getData(), UpmsUser.class);
            if (user.getUserName().equals("admin")){
                return "index";
            }
            model.addAttribute("message","非系统管理员！");
            return "login";
        }
        //登录页信息
        loadLoginSetting(model);
        return "login";
    }
}
